<template>
    <view class="forum-footer flex-between">
        <view class="share flex-center gap-[10rpx]" @click.stop="sharePost(item)">
            <image :src="$getImg('/img/icon/home/fenxiang.png')"></image>
            分享
        </view>

        <view class="comment flex-row gap-[30rpx]">
            <view class="flex-center gap-[10rpx]" @click.stop="commentPost(item)">
                <image :src="$getImg('/img/icon/home/pinlun.png')"></image>
                {{ item.commentCount }}
            </view>
            <view class="flex-center gap-[10rpx]">
                <image
                    @click.stop="likePost(item)"
                    :src="
                        item.userBehaviorEcho && item.userBehaviorEcho.likeFlag
                            ? $getImg('/img/icon/home/dianzan1.png')
                            : $getImg('/img/icon/home/dianzan.png')
                    "
                ></image>
                {{ item.likeCount }}
            </view>
            <view class="flex-center gap-[10rpx]">
                <image
                    @click.stop="collectPost(item)"
                    :src="
                        item.userBehaviorEcho && item.userBehaviorEcho.collectFlag
                            ? $getImg('/img/icon/home/shoucang1.png')
                            : $getImg('/img/icon/home/shoucang.png')
                    "
                ></image>
                {{ item.collectionCount }}
            </view>
        </view>
    </view>
</template>
<script setup lang="ts">
import { collectPostAPI, likePostAPI, sharePostAPI } from '@/api/posts'
import { userActionAPI } from '@/api/common'
const props = defineProps({
    item: {
        type: Object,
        default: () => ({
            userBehaviorEcho: {}
        })
    },
    // 3. 帖子  0 产品
    type: {
        type: String,
        default: '3'
    }
})

const emit = defineEmits(['showComment', 'share'])

// 分享
const sharePost = (row: any) => {
    emit('share', row)
}

// 点赞
const likePost = (row: any) => {
    userActionAPI({
        type: '1',
        action: '1' + props.type,
        bindId: row.id
    }).then((res) => {
        if (res.code === 0) {
            row.userBehaviorEcho.likeFlag = !row.userBehaviorEcho.likeFlag
            row.likeCount = row.userBehaviorEcho.likeFlag
                ? props.item.likeCount + 1
                : props.item.likeCount - 1

            console.log(row)
        }
    })
}

// 收藏
const collectPost = (row: any) => {
    userActionAPI({
        type: '2',
        action: '2' + props.type,
        bindId: row.id
    }).then((res) => {
        if (res.code === 0) {
            row.userBehaviorEcho.collectFlag = !row.userBehaviorEcho.collectFlag
            row.collectionCount = row.userBehaviorEcho.collectFlag
                ? props.item.collectionCount + 1
                : props.item.collectionCount - 1
        }
    })
}

const commentPost = (item: any) => {
    emit('showComment', item)
}
</script>

<style scoped lang="scss">
.forum-footer {
    .share {
        image {
            width: 40rpx;
            height: 40rpx;
        }
    }

    image {
        width: 30rpx;
        height: 30rpx;
    }
}
</style>
